package org.shangrila.comm.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class Test {

	public static void main(String[] args) {
		
		//saveFile();
		//listFilesAndDirs("d:/ftp");
		//listPathFiles("d:\\ftp\\");
		//listFile("d:/ftp"); // list all files in d:/ftp 
		
	}
	
	//只读取参数路径下的目录或者文件
    public static void listFilesAndDirs(String path) {
	  File dir = new File(path);
	  File[] files = dir.listFiles();
	  for(int i=0;i<files.length;i++){
	      System.out.println((files[i].isDirectory()?"目录：":"文件：")+files[i].getName());
	  	}
    }
    
    //只读取参数路径下的文件
    public static List<String> listPathFiles(String path) {
    	List<String> list = new ArrayList<String>();
    	File dir = new File(path);
	    File[] files = dir.listFiles();
	    for (File file : files) {
		  if (file.isFile()) {
			  System.out.println(file.getPath()+"--"+file.getName());
			  list.add(file.getPath());
			  }		  
	    }
	    return list;
    } 
    //递归法读取参数路径下的所有文件
    @SuppressWarnings("unused")
	private static void listFilesRecursion(String path) {
        File f = new File(path); 
        File[] fs = f.listFiles(); 
        if (fs == null) {
            return;
        } 
        for (File file : fs) {
            if (file.isFile()) {
                System.out.println(file.getPath());
            } else {
            	listFilesRecursion(file.getPath());
            }
        }
    }
    
    public static void saveFile() {
	try {
		InputStream inputStream = new FileInputStream("d:\\ftp\\wssoap.jar");
		saveInputStreamAsFile0(inputStream,"d:\\ftp\\wssoap.yyy");
		System.out.println("OK");
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}
    }
    public static void saveInputStreamAsFile(InputStream is,String fileName) {
		//InputStream to File
		//InputStream inputStream = new FileInputStream("d:\\ftp\\wssoap.jar");
		try {
			OutputStream os = new FileOutputStream(fileName);
			int byteCount = 0;
			byte[] buffer = new byte[1024];
			while ((byteCount = is.read(buffer)) != -1)	{
		          os.write(buffer, 0, byteCount);
			}
			is.close();
			os.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
    }

    public static void saveInputStreamAsFile0(InputStream inputStream,String fileName) {
		//InputStream to File
		//InputStream inputStream = new FileInputStream("file.xml");
		try {
			OutputStream outputStream = new FileOutputStream(fileName);
			int bytesWritten = 0;
			int byteCount = 0;
			byte[] bytes = new byte[1024];
			while ((byteCount = inputStream.read(bytes)) != -1)	{
		          outputStream.write(bytes, bytesWritten, byteCount);
		          bytesWritten += byteCount;
			}
			inputStream.close();
			outputStream.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
    }

}
